<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="50%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="设施组名称" prop="facilityGroupName">
                <el-input v-model="ruleForm.facilityGroupName" placeholder="请输入设施组名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="养殖类型" prop="breedType">
                <el-select :disabled="addEditFlag == 'lookView'" v-model="ruleForm.breedType" class="w100p" clearable
                    filterable placeholder="请选择养殖类型">
                    <el-option v-for="item, index in breedTypeList" :key="index" :label="item.name"
                        :value="item.breedType"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="筛选设施">
                <el-input v-model="ruleForm.name" placeholder="请输入设施名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="选择设施" prop="scope">
                <div class="tag"
                    v-show="(item.facilityName.indexOf(ruleForm.name || '') != -1 && ruleForm.breedType && ruleForm.breedType == item.breedType) || !ruleForm.breedType"
                    v-for="item, index in groupList" @click="chooseFacility(item, index)" :key="index">{{
                        item.facilityName }}
                </div>
                <div
                    v-if="groupList.filter(item => { return item.facilityName.indexOf(ruleForm.name || '') != -1 && ruleForm.breedType == item.breedType || !ruleForm.breedType }).length == 0">
                    暂无未分组的设施！</div>
            </el-form-item>
            <el-form-item label="已选择设施" prop="unitNo">
                <div class="tag" v-show="ruleForm.breedType == item.breedType || !ruleForm.breedType"
                    v-for="item, index in selectFacility" @click="delFacility(item, index)" :key="index">{{
                        item.facilityName }}
                </div>
                <div
                    v-if="selectFacility.filter(item => { return ruleForm.breedType == item.breedType || !ruleForm.breedType }).length == 0">
                    暂未选择设施！
                </div>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import facilityGroup from '@/api/digtalOperation/facilityGroup';
import facilityManagement from '@/api/digtalOperation/facilityManagement';
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        notGroupList: {
            type: Array,
            default: () => {
                return []
            }
        },
        groupFacility: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {

        return {
            breedTypeList: [
                {
                    name: "吊水养殖",
                    breedType: "HANGING_WATER_BREED"
                },
                {
                    name: "传统养殖",
                    breedType: "TRADITION_BREED"
                },
                {
                    name: "循环水养殖",
                    breedType: "CIRCULATE_BREED"
                },
                {
                    name: "跑道养殖",
                    breedType: "RUNWAY_BREED"
                }
            ],
            isShowCloseBtn: true,
            addEditName: '添加',
            facilityPointList: [],
            mapVisible: false,
            groupList: JSON.parse(JSON.stringify(this.notGroupList)),
            selectFacility: JSON.parse(JSON.stringify(this.groupFacility)),
            addEditApi: facilityGroup.add,
            ruleForm: {},
            unitList: [],
            facilityId: '',
            rules: {
                facilityGroupName: [
                    { required: true, message: "请选择设施类型", trigger: "change" }
                ],
                breedType: [
                    { required: true, message: "请选择类型", trigger: "change" }
                ],
            }
        }
    },
    computed: {
    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    mounted() { },
    methods: {
        async initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = facilityGroup.add
                this.addEditName = '添加'
                this.isShowCloseBtn = true;
            } else if (this.addEditFlag == 'edit' || this.addEditFlag == 'lookView') {
                this.ruleForm = { ...this.data }
                this.addEditApi = facilityGroup.edit
                this.addEditName = this.addEditFlag == 'lookView' ? '查看' : '编辑';
                this.isShowCloseBtn = this.addEditFlag == 'lookView' ? false : true;
            }
            this.$set(this, 'groupList', JSON.parse(JSON.stringify(this.notGroupList)));
            this.$set(this, 'selectFacility', JSON.parse(JSON.stringify(this.groupFacility)));
            console.log(this.notGroupList)
        },
        chooseFacility(item, index) {
            this.groupList.splice(index, 1);
            this.selectFacility.push(item);
            this.getSelectFacility()
        },
        delFacility(item, index) {
            this.selectFacility.splice(index, 1)
            this.groupList.push(item)
            this.getSelectFacility()
        },
        editIsDistribution() {
            let pormise = [];
            let that = this;
            this.groupList.forEach(item => {
                pormise.push(facilityManagement.edit({ ...item, isDistribution: false }))
            });
            this.selectFacility.forEach(item => {
                pormise.push(facilityManagement.edit({ ...item, isDistribution: true }))
            });
            Promise.all(pormise).then(reslist => {
                console.log(reslist)
                that.$store.dispatch('initData/queryAllFacilityDetail');
            }).catch(errList => {
                console.log(errList)
            })
        },
        getSelectFacility() {
            this.$set(this.ruleForm, 'facilityNoList', [])
            this.selectFacility.forEach(item => {
                if (item.breedType == this.ruleForm.breedType || !this.ruleForm.breedType) {
                    this.ruleForm.facilityNoList.push(item.facilityNo)
                }
            })
        },
        //提交表单
        submitForm() {
            // this.editIsDistribution();
            if (this.ruleForm.facilityNoList && this.ruleForm.facilityNoList.length > 0) {
                this.$refs.ruleForm.validate((valid) => {
                    console.log(this.ruleForm)
                    if (valid) {
                        this.addEditApi({ ...this.ruleForm }).then(res => {
                            if (res.code == 1) {
                                this.$message.success('操作成功')
                                this.$store.dispatch('initData/queryFacilityGroup');
                            } else {
                                this.$message.warning(res.msg || '操作失败')
                            }
                            this.cancel()
                        }).catch(err => {
                            this.$message.warning(err || '操作失败')
                        })
                    } else {
                        return false;
                    }
                })
            } else {
                this.$message.warning('请选择分组设施')
            }

        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.groupList = [];
            this.selectFacility = [];
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.axis {
    display: inline-block;
    padding: 0 20px;
}

.tag {
    padding: 5px 20px;
    background: #01c1f1;
    margin-bottom: 5px;
    margin-right: 10px;
    color: #ffffff;
    font-size: 14px;
    cursor: pointer;
    display: inline-block;
    border-radius: 6px;

    &:hover {
        background: #04728d;
    }
}
</style>
  